package com.xhwl.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.common.enums.AppTypeEnum;
import com.xhwl.common.enums.space.ViewType;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.App;
import com.xhwl.common.pojo.cent.sdata.OrganizationType;
import com.xhwl.common.query.cent.sdata.OrganizationTypeQuery;
import com.xhwl.common.utils.StringUtils;
import com.xhwl.data.service.IOrganizationTypeService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: luyi
 * @Description: 组织类型 包含 空间 自定义空间等
 * @Date: Create in 15:28 2021/8/2
 */
@RestController
public class OrganizationTypeController {

    @Autowired
    private IOrganizationTypeService organizationTypeService;

    @RequestMapping(value = "/org/type/insert")
    public ResultJson insert(@RequestBody OrganizationType organizationType){
        if(StringUtils.isEmpty(organizationType.getName()) || organizationType.getName().length() > 32){
            return ResultJson.fail("名称输入错误");
        }
        if(StringUtils.isNotEmpty(organizationType.getCode()) && organizationType.getCode().length() > 20){
            return ResultJson.fail("编码输入错误");
        }
        //添加空间类型可以不需要数据源头说明和说明
//        if(StringUtils.isNotEmpty(organizationType.getSource()) && organizationType.getSource().length() > 1000){
//            return ResultJson.fail("source 错误");
//        }
        if(StringUtils.isNotEmpty(organizationType.getRemark()) && organizationType.getRemark().length() > 250){
            return ResultJson.fail("remark 错误");
        }

        // 名称编码重复校验
        if(null == organizationType.getId()){
            QueryWrapper<OrganizationType> organizationTypeQueryWrapper = new QueryWrapper<>();
            organizationTypeQueryWrapper.eq("name", organizationType.getName());
            List<OrganizationType> list = organizationTypeService.list(organizationTypeQueryWrapper);
            if(!CollectionUtils.isEmpty(list)){
                return ResultJson.fail("名称重复");
            }
            QueryWrapper<OrganizationType> organizationTypeQueryWrapper1 = new QueryWrapper<>();
            organizationTypeQueryWrapper1.eq("code", organizationType.getName());
            List<OrganizationType> list1 = organizationTypeService.list(organizationTypeQueryWrapper);
            if(!CollectionUtils.isEmpty(list1)){
                return ResultJson.fail("编码重复");
            }
        }else{
            QueryWrapper<OrganizationType> organizationTypeQueryWrapper = new QueryWrapper<>();
            organizationTypeQueryWrapper.eq("name", organizationType.getName());
            organizationTypeQueryWrapper.ne("id", organizationType.getId());
            List<OrganizationType> list = organizationTypeService.list(organizationTypeQueryWrapper);
            if(!CollectionUtils.isEmpty(list)){
                return ResultJson.fail("名称重复");
            }
            QueryWrapper<OrganizationType> organizationTypeQueryWrapper1 = new QueryWrapper<>();
            organizationTypeQueryWrapper1.eq("code", organizationType.getName());
            organizationTypeQueryWrapper1.ne("id", organizationType.getId());
            List<OrganizationType> list1 = organizationTypeService.list(organizationTypeQueryWrapper);
            if(!CollectionUtils.isEmpty(list1)){
                return ResultJson.fail("编码重复");
            }
        }

        if(null == organizationType.getId()){
            organizationTypeService.save(organizationType);
        }else{
            organizationTypeService.updateById(organizationType);
        }
        return ResultJson.success();
    }

    /**
     * 组织类型
     * @param organizationTypeQuery
     * @return
     * 注： 1 运维平台和业务平台都可能调用，故不能将请求头中的业态信息作为查询条件
     *     2 运管平台需要展示所有的空间类型，企业业态的平台只展示给业态配置的类型
     */
    @RequestMapping(value = "/org/type/list")
    public ResultJson list(@RequestBody OrganizationTypeQuery organizationTypeQuery, @RequestHeader short industryId, App app){
    	if(null == organizationTypeQuery.getIndustryId()){
			organizationTypeQuery.withIndustryId(industryId);
		}
        List<OrganizationType> organizationTypeList = organizationTypeService.list(organizationTypeQuery);
        return ResultJson.success(organizationTypeList);
    }

    /**
     * 组织类型
     * @return
     */
    @RequestMapping(value = "/org/type/list/v2")
    public ResultJson listV2(@RequestBody OrganizationTypeQuery organizationTypeQuery){
        List<OrganizationType> organizationTypeList = organizationTypeService.list(organizationTypeQuery);
        return ResultJson.success(organizationTypeList);
    }

    @RequestMapping(value = "/org/type/one")
    public ResultJson one(Integer id){
		QueryWrapper<OrganizationType> organizationTypeQueryWrapper = new QueryWrapper<>();
		organizationTypeQueryWrapper.eq("id", id);
		OrganizationType one = organizationTypeService.getOne(organizationTypeQueryWrapper);
		return ResultJson.success(one);
	}

    @RequestMapping(value = "/org/type/page")
    public ResultJson page(@RequestBody OrganizationTypeQuery organizationTypeQuery, Page page){
        Page<OrganizationType> result = organizationTypeService.page(organizationTypeQuery, page);
        return ResultJson.success(result);
    }

    @RequestMapping(value = "/org/type/delete")
    public ResultJson delete(Integer id){
        boolean b = organizationTypeService.removeById(id);
        return ResultJson.success("删除成功", b);
    }


    /**
     * 查看view-type
     * @return
     */
    @RequestMapping(value = "/org/type/view-type")
    public ResultJson viewType(){
        ViewType[] values = ViewType.values();
        List<Map<String, String>> result = new ArrayList<>(values.length);
        for (ViewType value : values) {
            Map<String, String> map = new HashMap<>();
            map.put("k", value.name());
            map.put("v", value.desc);
            result.add(map);
        }
        return ResultJson.success(result);
    }

}
